﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;
using System.Web.UI;
using System.Web;

namespace TST.SharePointObjects
{
    public class VersionInfo
    {
        private int _version;
        private string _createdBy;
        private DateTime _created;
        private string _licenseKey;
        private Licensing _license;
        private SharePointObjectsDatabase _database;

        internal VersionInfo(SharePointObjectsDatabase database)
        {
            _database = database;
        }

        internal int DatabaseVersion
        {
            get
            {
                if (_version == 0)
                    GetVersionInfo();
                return _version;
            }
        }

        internal string CreatedBy
        {
            get
            {
                if (_createdBy == null)
                    GetVersionInfo();
                return _createdBy;
            }
        }

        internal DateTime Created
        {
            get
            {
                if (_created == null)
                    GetVersionInfo();
                return _created;
            }
        }

        internal string LicenseKey
        {
            get
            {
                if (_licenseKey == null)
                    GetVersionInfo();
                return _licenseKey;
            }
        }

        internal Licensing License
        {
            get
            {
                if (_license == null)
                    GetVersionInfo();
                return _license;
            }
        }

        private void GetVersionInfo()
        {
            _license = new Licensing(string.Empty);
            SqlCommand command = new SqlCommand();
            command.CommandText = "GetVersionInfo";

            SqlParameter paramID = new SqlParameter("@FarmID", SqlDbType.UniqueIdentifier);
            paramID.Value = SPFarm.Local.Id;
            command.Parameters.Add(paramID);

            try
            {
                DataTable data = _database.GetData(command, CommandType.StoredProcedure);
                if (data != null && data.Rows.Count == 1)
                {
                    DataRow row = data.Rows[0];
                    object dbVersion = row["Version"];
                    _version = Convert.ToInt32(dbVersion);
                    _createdBy = row["CreatedBy"].ToString();
                    _created = (DateTime)row["Created"];
                    if (row["LicenseKey"] == null)
                    {
                        _licenseKey = string.Empty;
                    }
                    else
                    {
                        _licenseKey = row["LicenseKey"].ToString();
                    }
                    _license = new Licensing(_licenseKey);
                }
            }
            catch (SharePointObjectsException)
            {
                // table does not yet exist.
                _version = -1;
            }
        }

        internal void RegisterVersion(int version)
        {
            SqlCommand command = new SqlCommand("SetVersionInfo");

            SqlParameter paramID = new SqlParameter("@FarmID", SqlDbType.UniqueIdentifier);
            paramID.Value = SPFarm.Local.Id;
            command.Parameters.Add(paramID);

            SqlParameter paramUser = new SqlParameter("@CreatedBy", SqlDbType.NVarChar, 50);
            paramUser.Value = HttpContext.Current.User.Identity.Name;
            command.Parameters.Add(paramUser);

            SqlParameter paramVersion = new SqlParameter("@Version", SqlDbType.TinyInt);
            paramVersion.Value = version;
            command.Parameters.Add(paramVersion);

            SqlParameter paramLicense = new SqlParameter("@LicenseKey", SqlDbType.NVarChar, 200);
            paramLicense.Value = Licensing.GetLicenseXML(version);
            command.Parameters.Add(paramLicense);

            _database.RunStoredProcedure(command);
            GetVersionInfo();
        }

    }
}
